import constant from "./constant";

export default {
    mobile() {
        let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i);
        return flag && flag.length && flag.length > 0;
    },

    /**
     * 判断是否为空
     */
    isEmpty(value) {
        return typeof value === "undefined"
            || value === null
            || (typeof value === "string" && value.trim() === "")
            || (Array.prototype.isPrototypeOf(value) && value.length === 0)
            || (Object.prototype.isPrototypeOf(value) && Object.keys(value).length === 0);
    },

    /**
     * 表情包转换
     */
    faceReg(content) {
        content = content.replace(/\[[^\[^\]]+\]/g, (word) => {
            let index = constant.emojiList.indexOf(word.replace("[", "").replace("]", ""));
            if (index > -1) {
                let url = constant.qiniuDownload + "emoji/q" + (index + 1) + ".gif";
                return '<img style="vertical-align: middle;width: 32px;height: 32px" src="' + url + '" title="' + word + '"/>';
            } else {
                return word;
            }
        });
        return content;
    },

    /**
     * 图片转换
     */
    pictureReg(content) {
        content = content.replace(/\[[^\[^\]]+\]/g, (word) => {
            let index = word.indexOf(",");
            if (index > -1) {
                let arr = word.replace("[", "").replace("]", "").split(",");
                return '<img class="pictureReg" style="border-radius: 5px;width: 100%;max-width: 250px;display: block" src="' + arr[1] + '" title="' + arr[0] + '"/>';
            } else {
                return word;
            }
        });
        return content;
    },

    imgShow(select) {
        $(select).click(function () {
            let src = $(this).attr("src");
            $("#bigImg").attr("src", src);

            /** 获取当前点击图片的真实大小，并显示弹出层及大图 */
            $("<img/>").attr("src", src).load(function () {
                let windowW = $(window).width();//获取当前窗口宽度
                let windowH = $(window).height();//获取当前窗口高度
                let realWidth = this.width;//获取图片真实宽度
                let realHeight = this.height;//获取图片真实高度
                let imgWidth, imgHeight;
                let scale = 0.8;//缩放尺寸，当图片真实宽度和高度大于窗口宽度和高度时进行缩放

                if (realHeight > windowH * scale) {//判断图片高度
                    imgHeight = windowH * scale;//如大于窗口高度，图片高度进行缩放
                    imgWidth = imgHeight / realHeight * realWidth;//等比例缩放宽度
                    if (imgWidth > windowW * scale) {//如宽度仍大于窗口宽度
                        imgWidth = windowW * scale;//再对宽度进行缩放
                    }
                } else if (realWidth > windowW * scale) {//如图片高度合适，判断图片宽度
                    imgWidth = windowW * scale;//如大于窗口宽度，图片宽度进行缩放
                    imgHeight = imgWidth / realWidth * realHeight;//等比例缩放高度
                } else {//如果图片真实高度和宽度都符合要求，高宽不变
                    imgWidth = realWidth;
                    imgHeight = realHeight;
                }
                $("#bigImg").css("width", imgWidth);//以最终的宽度对图片缩放

                let w = (windowW - imgWidth) / 2;//计算图片与窗口左边距
                let h = (windowH - imgHeight) / 2;//计算图片与窗口上边距
                $("#innerImg").css({"top": h, "left": w});//设置top和left属性
                $("#outerImg").fadeIn("fast");//淡入显示
            });

            $("#outerImg").click(function () {//再次点击淡出消失弹出层
                $(this).fadeOut("fast");
            });
        });
    },
}
